home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / text / misc / EIQuotesV2.lha / quotes / Quotes.c < prev    next >
C/C++ Source or Header  |  1992-09-02  |  5KB  |  131 lines

  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  2. /*    OOOOOOO   (tm)                                                         */
  3. /*  OOOOOOOOOOO                Copyright © 1993 Engineering Ideas.           */
  4. /* OOOOOOOOOOOOO                       All Rights Reserved.                  */
  5. /* OOOOOOOOOOOOO                                                             */
  6. /*  OOOOOOOOOOO  OOO        This program may not be distributed without      */
  7. /*    OOOOOOO  OOOOOOO          prior permission from the author(s):         */
  8. /*            OOOOOOOOO                  Richard Bemrose                     */
  9. /*             OOOOOOO                                                       */
  10. /*           OO  OOO                    Engineering Ideas                    */
  11. /*         OOOOOO               22 Keppel View Road, Kimberworth,            */
  12. /*           OO                   Rotherham, S. Yorks., S61 2AS,             */
  13. /*                                          ENGLAND                          */
  14. /*                                                                           */
  15. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  16. #include <intuition/intuition.h>
  17. #include <intuition/intuitionbase.h>
  18. #include <libraries/dos.h>
  19. #include <libraries/ppbase.h>
  20. #include <exec/types.h>
  21. #include <exec/memory.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #ifdef LATTICE
  25. #include <proto/all.h>
  26. int CXBRK(void) {return(0);}
  27. #endif
  28. #include <proto/powerpacker.h>
  29.  
  30. #define ERR_INTUITION   1
  31. #define ERR_NOPPLIB     2
  32. #define ERR_NOARGS      3
  33. #define ERR_PPMSG       4
  34. #define ERR_FILE        5
  35.  
  36. struct PPBase *PPBase = NULL;
  37. struct IntuitionBase *IntuitionBase = NULL;
  38.  
  39. void clean_up(UBYTE );
  40.  
  41. UBYTE *file = NULL;                              /* quotes file storage   */
  42. ULONG filelen;                                   /* length of file        */
  43. UBYTE pperror;                                   /* powerpacker error no. */
  44.  
  45. void main(int argc, char *argv[])
  46.     {
  47.     UBYTE *ptr;
  48.     UWORD rand_num, num_quotes=0;
  49.     ULONG seconds, micros;
  50.  
  51.     if ( !(IntuitionBase = (struct IntuitionBase *) OpenLibrary( "intuition.library", 0 )) )
  52.             clean_up(ERR_INTUITION);
  53.  
  54.     if ( !(PPBase = (struct PPBase *) OpenLibrary ("powerpacker.library", 0L)) )
  55.             clean_up(ERR_NOPPLIB);
  56.  
  57.     if (argc!=2)
  58.             clean_up(ERR_NOARGS);
  59.  
  60.     /* Powerpackers clever stuff! Loads packed file then decruches it automatically */
  61.     pperror = ppLoadData (argv[1], DECR_NONE, MEMF_PUBLIC, &file, &filelen, NULL);
  62.     if (pperror)
  63.             clean_up(ERR_PPMSG);
  64.  
  65.     /* Collects a random seed number from micro seconds */
  66.     CurrentTime( &seconds, µs );
  67.     srand(micros);
  68.  
  69.     ptr=file;
  70.  
  71.     while ( (*ptr++)!='\0' )                         /* Counts no. of quotes */
  72.             if ( *ptr=='#' )
  73.                     num_quotes++;
  74.  
  75.     if ( num_quotes )                                /* Any quotes in file   */
  76.             {
  77.             rand_num=rand()%num_quotes;              /* Picks random quote   */
  78.  
  79.             ptr=file;
  80.  
  81.             while ( rand_num>0 )                     /* Jumps to quote       */
  82.                     if ( (*ptr++)=='#' )
  83.                             rand_num--;
  84.  
  85.             while ( *ptr!='#' )                      /* Prints quote         */
  86.                     putchar(*ptr++);
  87.             }
  88.     else
  89.             clean_up(ERR_FILE);
  90.     puts(NULL);
  91.     clean_up(0);                                     /* Close everything     */
  92.     }
  93.  
  94.  
  95. void clean_up(UBYTE err)
  96.     {
  97.     if ( err )
  98.             puts ("Quotes V2  (c) 1993 Engineering Ideas.");
  99.  
  100.     switch ( err )                                   /* Error messages       */
  101.             {
  102.             case ERR_INTUITION:
  103.                     puts ("Can not open Intuition Library.");
  104.                     break;
  105.             case ERR_NOPPLIB:
  106.                     puts ("You need powerpacker.library V33+ !");
  107.                     break;
  108.             case ERR_NOARGS:
  109.                     puts ("USAGE: Quotes <path/file>\n");
  110.                     break;
  111.             case ERR_PPMSG:
  112.                     printf ("Error: %s\n", ppErrorMessage (pperror));
  113.                     break;
  114.             case ERR_FILE:
  115.                     puts ("No quotes found in file. Insert # after each quote.");
  116.             default:
  117.                     break;
  118.             }
  119.  
  120.     if ( file )                                      /* A *must* for powerpacker */
  121.             FreeMem (file, filelen);
  122.  
  123.     if ( PPBase )
  124.             CloseLibrary ((struct Library *)PPBase);
  125.  
  126.     if ( IntuitionBase )
  127.             CloseLibrary( (struct Library *)IntuitionBase );
  128.  
  129.     exit(0);
  130.     }
  131.